package com.jeeplus.test.jlnu.admissionInfo.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyWebSocketHandler extends TextWebSocketHandler {

    private final Map<String, WebSocketSession> sessions = new HashMap<>();
    private final ObjectMapper objectMapper = new ObjectMapper(); // 用于将对象转换为 JSON

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        sessions.put(session.getId(), session);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        sessions.remove(session.getId());
    }

    public void sendProgressUpdate(int progress) throws IOException {
        // 创建包含进度信息的 Map
        Map<String, Object> progressData = new HashMap<>();
        progressData.put("progress", progress);

        // 将 Map 转换为 JSON 字符串
        String jsonMessage = objectMapper.writeValueAsString(progressData);

        TextMessage message = new TextMessage(jsonMessage);

        // 向所有打开的 WebSocket 会话发送消息
        for (WebSocketSession session : sessions.values()) {
            if (session.isOpen()) {
                session.sendMessage(message);
            }
        }
    }

    // 获取特定 sessionId 对应的 WebSocketSession
    public WebSocketSession getWebSocketSession(String sessionId) {
        return sessions.get(sessionId); // 获取特定 sessionId 的 WebSocketSession
    }

    // 获取第一个连接的 WebSocketSession
    public WebSocketSession getFirstWebSocketSession() {
        if (!sessions.isEmpty()) {
            return sessions.values().iterator().next(); // 返回第一个会话
        }
        return null;
    }

    // 获取所有 WebSocketSession
    public Map<String, WebSocketSession> getAllSessions() {
        return sessions;
    }

    public void sendMsgList(List<String> msg) throws IOException {
        // 创建包含进度信息的 Map
        Map<String, Object> progressData = new HashMap<>();
        progressData.put("msg", msg);

        // 将 Map 转换为 JSON 字符串
        String jsonMessage = objectMapper.writeValueAsString(progressData);

        TextMessage message = new TextMessage(jsonMessage);

        // 向所有打开的 WebSocket 会话发送消息
        for (WebSocketSession session : sessions.values()) {
            if (session.isOpen()) {
                session.sendMessage(message);
            }
        }
    }

    public void sendMsg(String msg) throws IOException {
        // 创建包含进度信息的 Map
        Map<String, Object> progressData = new HashMap<>();
        progressData.put("msg", msg);

        // 将 Map 转换为 JSON 字符串
        String jsonMessage = objectMapper.writeValueAsString(progressData);

        TextMessage message = new TextMessage(jsonMessage);

        // 向所有打开的 WebSocket 会话发送消息
        for (WebSocketSession session : sessions.values()) {
            if (session.isOpen()) {
                session.sendMessage(message);
            }
        }
    }
}
